using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using WozLib.WozCommands;
using WozLib.WozObjects;

namespace WOzTestUI.WozCommands
{
  public class WozServerClientSendCommand : WozObjectWozCommand
  {

    [XmlAttribute("WozServerCommand")]
    public WozLib.WozCommands.WozCommand WozServerCommand
    {
      get
      {
        return _WozServerCommand;
      }
      set
      {
        _WozServerCommand = value;
      }
    } WozLib.WozCommands.WozCommand _WozServerCommand;


    public WozServerClientSendCommand()
    {
    }

    public WozServerClientSendCommand(WizardWindow wizardWindow, WozLib.WozCommands.WozCommand wozServerCommand, bool sendToClient)
      : base (wizardWindow)
    {
      WozServerCommand = wozServerCommand;

      if (sendToClient)
      {
        if (wizardWindow != null && 
        (WozServerCommand is ClientWozCommand ||
         (WozServerCommand is SketchPadWozCommand &&
         (WozServerCommand as SketchPadWozCommand).WozServerCommand is ClientWozCommand)))
        {
          if (WozServerCommand is SketchPadWozCommand)
          {
            wizardWindow.SendClientCommand(((WozServerCommand as SketchPadWozCommand).WozServerCommand as ClientWozCommand).WozClientCommands);
          }
          else if (WozServerCommand is ClientWozCommand)
          {
            wizardWindow.SendClientCommand((WozServerCommand as ClientWozCommand).WozClientCommands);
          }
        }
      }
    }

    /// <summary>
    /// Returns the WizardWindow that is the target of this command. Returns null if none exists.
    /// </summary>
    [XmlIgnore]
    public WizardWindow TargetWizardWindow
    {
      get
      {
        IServerObject iServerObject = TargetIServerObject;
        if (iServerObject is WizardWindow)
        {
          return iServerObject as WizardWindow;
        }
        else
        {
          return null;
        }
      }
      set
      {
        TargetServerID = value.ServerID;
      }
    }

    public override void Undo()
    {
      WozServerCommand.Undo();

      WizardWindow wizardWindow = TargetWizardWindow;

      if (wizardWindow != null &&
        (WozServerCommand is ClientWozCommand ||
         (WozServerCommand is SketchPadWozCommand &&
         (WozServerCommand as SketchPadWozCommand).WozServerCommand is ClientWozCommand)))
      {
        if (WozServerCommand is SketchPadWozCommand)
        {
          wizardWindow.SendClientCommand(((WozServerCommand as SketchPadWozCommand).WozServerCommand as ClientWozCommand).WozClientCommands.UndoCommand());
        }
        else if (WozServerCommand is ClientWozCommand)
        {
          wizardWindow.SendClientCommand((WozServerCommand as ClientWozCommand).WozClientCommands.UndoCommand());
        }
      }
    }

    public override void Redo()
    {
      WozServerCommand.Redo();

      WizardWindow wizardWindow = TargetWizardWindow;

      if (wizardWindow != null &&
        (WozServerCommand is ClientWozCommand ||
         (WozServerCommand is SketchPadWozCommand &&
         (WozServerCommand as SketchPadWozCommand).WozServerCommand is ClientWozCommand)))
      {
        if (WozServerCommand is SketchPadWozCommand)
        {
          wizardWindow.SendClientCommand(((WozServerCommand as SketchPadWozCommand).WozServerCommand as ClientWozCommand).WozClientCommands);
        }
        else if (WozServerCommand is ClientWozCommand)
        {
          wizardWindow.SendClientCommand((WozServerCommand as ClientWozCommand).WozClientCommands);
        }
      }
    }
  }
}
